﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Components.Enum;
using Model;
using MsDAL;
using System.ComponentModel;
using ServiceHelper.Doc;
using ServiceHelper.Util;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Bas1a22Manager : Bas1a22Base
    {
        private static Bas1a22Manager _entity;

        public static Bas1a22Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Bas1a22Manager();
                return _entity;
            }
        }

        public string SaveData(Hashtable tb, string action, string usercode)
        {
            var shopid = tb["shopid"].ToString();

            var bas120 = Bas1a20Manager.Entity.GetModelById(shopid);

            var list = new List<SqlApp>();
            
            if (tb.ContainsKey(ShopSetting.DirectStock))
                list.Add(CreateOrUpdate(bas120, ShopSetting.DirectStock, tb[ShopSetting.DirectStock].ToString()));

            if (tb.ContainsKey(ShopSetting.NeedAudit))
                list.Add(CreateOrUpdate(bas120, ShopSetting.NeedAudit, tb[ShopSetting.NeedAudit].ToString()));

            if (tb.ContainsKey(ShopSetting.PrintPrice))
                list.Add(CreateOrUpdate(bas120, ShopSetting.PrintPrice, tb[ShopSetting.PrintPrice].ToString()));

            return CommonManager.Entity.SaveData(list);
        }

        private SqlApp CreateOrUpdate(Bas1a20 a120, string settingName, string settingValue)
        {
            var where = QB.Entity
                .AddParam("@sellerid", a120.Slistid)
                .AddParam("@shopid", a120.Listid)
                .AddParam("@setname", settingName);

            var model = this.GetModel("sellerid=@sellerid and shopid=@shopid and setname=@setname", where);

            var isAdd = false;
            if (null == model)
            {
                model = new Bas1a22();
                model.Listid = Sys0a60Manager.Entity.CreateSn("bas1a22_listid", ServerContext.UserCode);
                isAdd = true;
            }

            model.Sellerid = a120.Slistid;
            model.Shopid = a120.Listid;
            model.Sellerid = a120.Slistid;
            model.Setname = settingName;
            model.Value = settingValue;

            return isAdd == true ? this.Add(model) : this.Update(model);
        }
    }
}



